package models

import (
	"devops/pkg/git/git_model"
	"time"
)

type CI struct {
	Model
	ProjectId          int    `gorm:"column:project_id" db:"column:project_id" json:"project_id" form:"project_id"`                                     //项目ID
	Name               string `gorm:"column:name" db:"column:name" json:"name" form:"name"`                                                             // 项目名称
	ScmType            string `gorm:"column:scm_type" db:"column:scm_type" json:"scm_type" form:"scm_type"`                                             // 代码仓库类型
	RepositoryUrl      string `gorm:"column:repository_url" db:"column:repository_url" json:"repository_url" form:"repository_url"`                     // 仓库链接
	Branch             string `gorm:"column:branch" db:"column:branch" json:"branch" form:"branch"`                                                     // 仓库分支
	RepositoryUsername string `gorm:"column:repository_username" db:"column:repository_username" json:"repository_username" form:"repository_username"` // 仓库用户名
	RepositoryToken    string `gorm:"column:repository_token" db:"column:repository_token" json:"repository_token" form:"repository_token"`             // 仓库访问令牌
	CredentialsId      string `gorm:"column:credentials_id" db:"column:credentials_id" json:"credentials_id" form:"credentials_id"`                     // 凭证ID
	Jenkinsfile        string `gorm:"column:jenkinsfile" db:"column:jenkinsfile" json:"jenkinsfile" form:"jenkinsfile"`
	JenkinsfileType    int    `gorm:"column:jenkinsfile_type" db:"column:jenkinsfile_type" json:"jenkinsfile_type" form:"jenkinsfile_type"` // 环境设置id
	Repository         string `gorm:"column:repository" db:"column:repository" json:"repository" form:"repository"`                         // 仓库主目录
	RepositoryOwner    string `gorm:"column:repository_owner" db:"column:repository_owner" json:"repository_owner" form:"repository_owner"` // 仓库主目录所属
	RepositoryPath     string `gorm:"column:repository_path" db:"column:repository_path" json:"repository_path" form:"repository_path"`     // 仓库项目路径
	TemplateId         int    `gorm:"column:template_id" db:"column:template_id" json:"template_id" form:"template_id"`                     // 模版ID
	BaseURL            string `gorm:"column:base_url" db:"column:base_url" json:"base_url" form:"base_url"`                                 // 自建仓库的baseurl
}

type GetCIOneResp struct {
	CI                          // 构建计划基础信息
	Branches []git_model.Branch `json:"branches" form:"branches"` // git 库列表
	Repos    []git_model.Repo   `json:"repos" form:"repos"`       // git 分支列表
	Tmp      Template           `json:"tmp" form:"tmp"`           // 模版信息
}

type BuildAndStages struct {
	*Build
	Stages          []*BuildStageAndFlow `json:"stages"`
	DeployGroupID   string               `json:"deploy_group_id"`
	DeployGroupName string               `json:"deploy_group_name"`
	ImageName       string               `json:"image_name"`
}

type ProjectCI struct {
	*CI
	*BuildAndStages `json:"last_build"`
}

func CreateCI(ci *CI) error {
	return db.Create(ci).Error
}

func GetCI(projectID int, cid int) *CI {
	ci := &CI{}
	db.Where("deleted_at = 0").
		Where("project_id = ?", projectID).
		Where("id = ?", cid).
		First(ci)
	if ci.ID != cid {
		return nil
	}
	return ci
}

func UpdateCI(cid int, ci CI) error {
	// 零值问题
	if ci.JenkinsfileType == 0 {
		db.Model(&CI{}).Where("id = ?", cid).Updates(map[string]interface{}{"jenkinsfile_type": 0})
	}
	return db.Where("id = ?", cid).Updates(&ci).Error
}

func GetCIByID(id int) *CI {
	ci := &CI{}
	db.Where("deleted_at = 0").
		Where("id = ?", id).
		First(ci)
	if ci.ID == 0 {
		return nil
	}
	return ci
}

func GetCIByRepoUrl(url string) *CI {
	ci := &CI{}
	db.Where("deleted_at = 0").
		Where("repository_url = ?", url).
		First(ci)
	if ci.ID == 0 {
		return nil
	}
	return ci
}

func GetCIByPIDAndName(projectID int, name string) *CI {
	ci := &CI{}
	db.Where("deleted_at = 0").
		Where("project_id = ?", projectID).
		Where("name = ?", name).
		First(ci)
	if ci.ID == 0 {
		return nil
	}
	return ci
}

func GetProjectCI(projectID int) []*CI {
	cis := make([]*CI, 0)
	db.Where("deleted_at = 0").
		Where("project_id = ?", projectID).
		Find(&cis)
	return cis
}

func GetProjectCIPage(projectID int, page int, pageSize int) ([]*CI, int) {
	cis := make([]*CI, 0, pageSize)
	var total int64
	db.Model(&CI{}).Where("deleted_at = 0").
		Where("project_id = ?", projectID).
		Count(&total)
	db.Where("deleted_at = 0").
		Where("project_id = ?", projectID).
		Order("updated_at desc").
		Offset((page - 1) * pageSize).
		Limit(pageSize).
		Find(&cis)
	return cis, int(total)
}

func GetProjectCIPageSearch(projectID int, page int, pageSize int, search string) ([]*CI, int) {
	cis := make([]*CI, 0, pageSize)
	var total int64
	db.Model(&CI{}).Where("deleted_at = 0").
		Where("name Like ?", "%"+search+"%").
		Where("project_id = ?", projectID).
		Count(&total)
	db.Where("deleted_at = 0").
		Where("name Like ?", "%"+search+"%").
		Where("project_id = ?", projectID).
		Order("updated_at desc").
		Offset((page - 1) * pageSize).
		Limit(pageSize).
		Find(&cis)
	return cis, int(total)
}

func DeleteCI(id int) error {
	return db.Model(&CI{}).Where("id = ?", id).Update("deleted_at", time.Now().Unix()).Error
}
